import {nextTick} from 'vue'
import {loadCss, loadJs} from './commonUtil'
import * as elIcons from '@element-plus/icons-vue'

/**
 * 使用外部链接时注意跨域问题
 */
const cssUrls: string[] = [
	// iconfont项目中的图标
	// '//at.alicdn.com/t/c/font_4050867_699p2qyyhop.css'
	// 链接跨域，已改为本地引入，不再使用CDN
	// '//cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'
	// '//cdn.bootcdn.net/ajax/libs/font-awesome/6.3.0/css/all.css'
]
const jsUrls: string[] = []

/**
 * 加载预设的字体图标资源
 */
export default function init() {
	if (cssUrls.length > 0) {
		cssUrls.map((v) => {
			loadCss(v)
		})
	}
	if (jsUrls.length > 0) {
		jsUrls.map((v) => {
			loadJs(v)
		})
	}
}

/**
 * 获取当前页面中从指定域名加载到的样式表内容<br>
 * 样式表未载入前无法获取
 */
function getStylesFromHref(href: string) {
	const sheets = []
	const styles: StyleSheetList = document.styleSheets
	for (const key in styles) {
		if (styles[key].href && styles[key].href!.indexOf(href) > -1) {
			sheets.push(styles[key])
		}
	}
	return sheets
}

/**
 * 获取当前页面中指定title的样式表内容<br>
 * 样式表未载入前无法获取<br>
 * title是在link标签中设置的，项目打包后其值会变为null
 */
function getStylesByTitle(title: string) {
	const sheets = []
	const styles: StyleSheetList = document.styleSheets
	for (const key in styles) {
		if (styles[key].title === title) {
			sheets.push(styles[key])
		}
	}
	return sheets
}

/**
 * 获取 Element-Plus 自带的图标
 */
export function getElementPlusIconfontNames() {
	return new Promise<string[]>((resolve, reject) => {
		nextTick().then(() => {
			const iconFonts = []
			const icons = elIcons as any
			for (const i in icons) {
				iconFonts.push(`el-icon-${icons[i].name}`)
			}
			if (iconFonts.length > 0) {
				resolve(iconFonts)
			} else {
				reject('No ElementPlus Icons')
			}
		})
	})
}

/**
 * 获取 Awesome-Iconfont 的 name 列表
 */
export function getAwesomeIconfontNames() {
	return new Promise<string[]>((resolve, reject) => {
		nextTick().then(() => {
			const iconFonts = []
			// const sheets = getStylesFromHref('font-awesome.min.css')
			// const sheets = getStylesByTitle('iconStyleSheet')
			const sheets = document.styleSheets
			for (const key in sheets) {
				const rules: any = sheets[key].cssRules
				for (const k in rules) {
					if (rules[k].selectorText && /^\.fa-(.*)::before$/g.test(rules[k].selectorText)) {
						if (rules[k].selectorText.indexOf(', ') > -1) {
							const iconNames = rules[k].selectorText.split(', ')
							// 含图标别名
							// for (const i_k in iconNames) {
							// 	iconFonts.push(`${iconNames[i_k].substring(1, iconNames[i_k].length).replace(/\:\:before/gi, '')}`)
							// }
							iconFonts.push(`${iconNames[0].substring(1, iconNames[0].length).replace(/::before/gi, '')}`)
						} else {
							iconFonts.push(`${rules[k].selectorText.substring(1, rules[k].selectorText.length).replace(/::before/gi, '')}`)
						}
					}
				}
			}
			if (iconFonts.length > 0) {
				resolve(iconFonts)
			} else {
				reject('No AwesomeIcon style sheet')
			}
		})
	})
}

/**
 * 获取 Iconfont 的 name 列表
 */
export function getIconfontNames() {
	return new Promise<string[]>((resolve, reject) => {
		nextTick().then(() => {
			const iconFonts = []
			// const sheets = getStylesFromHref('iconfont.css')
			// const sheets = getStylesByTitle('iconStyleSheet')
			const sheets = document.styleSheets
			for (const key in sheets) {
				const rules: any = sheets[key].cssRules
				for (const k in rules) {
					// 在iconfont网站的项目设置中填写的前缀
					const prefix = 'icon-'
					if (rules[k].selectorText && new RegExp('^\\.' + prefix + '(.*)::before$', 'g').test(rules[k].selectorText)) {
						iconFonts.push(`${rules[k].selectorText.substring(1, rules[k].selectorText.length).replace(/::before/gi, '')}`)
					}
				}
			}
			if (iconFonts.length > 0) {
				resolve(iconFonts)
			} else {
				reject('No Iconfont style sheet')
			}
		})
	})
}

/**
 * 获取本地图标
 * src/assets/icons/svg/cus 文件夹内的svg文件
 */
export function getLocalIconfontNames() {
	return new Promise<string[]>((resolve, reject) => {
		nextTick().then(() => {
			let iconFonts: string[] = []
			const svgEl = document.getElementById('local-icon')
			if (svgEl?.dataset.iconName) {
				iconFonts = (svgEl?.dataset.iconName as string).split(',')
			}
			if (iconFonts.length > 0) {
				resolve(iconFonts)
			} else {
				reject('No Local Icons')
			}
		})
	})
}
